package com.manager.common.aspect;

import com.manager.common.annotation.AspectMethodOperation;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.utils.DataTools;
import com.manager.model.System_Setting;
import com.manager.system.service.SystemSettingService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;
import java.util.Map;

@Aspect
@Component
@SuppressWarnings("all")
public class SystemSettingAspect {

	@Pointcut("execution(public * com.manager.system.controller.SystemSettingController.*(..))")
	private void controllerAspect() {
	}

	@Autowired
	private SystemSettingService systemSettingService;

	@Before(value = "controllerAspect()")
	public void methodBefore(JoinPoint joinPoint) throws Exception {

	}

	@Around(value = "controllerAspect()")
	public Object timeAround(ProceedingJoinPoint joinPoint) throws Exception {
		// 定义返回对象、得到方法需要的参数
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		Object obj = null;
		Object[] args = joinPoint.getArgs();
		long startTime = System.currentTimeMillis();
		boolean mark = true;
		try {
			obj = joinPoint.proceed(args);
		} catch (Throwable e) {
			e.printStackTrace();
			mark = false;
		}

		if (mark) {
			MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
			Method method = methodSignature.getMethod();
			AspectMethodOperation info = method.getAnnotation(AspectMethodOperation.class);
			System_Setting systemSetting = (System_Setting) joinPoint.getArgs()[0];

			if (info != null) {

				synchronized (DataTools.systemSettingMap){
					String id = systemSetting.getId();
					if(StringUtils.isNotEmpty(id)){
						for(String key: DataTools.systemSettingMap.keySet()){
							System_Setting oldValue =DataTools.systemSettingMap.getOrDefault(key,new System_Setting());
							if(id.equals(oldValue.getId())){
								DataTools.systemSettingMap.remove(oldValue.getSystemKey());
							}
						}
					}
					if (info.operation() == AspectMethodOperationEnum.ADD
							|| info.operation() == AspectMethodOperationEnum.SAVE
							|| info.operation() == AspectMethodOperationEnum.UPDATE) {
						DataTools.systemSettingMap.putIfAbsent(systemSetting.getSystemKey(), systemSetting);

					}
				}

			}
		}
		Map<String,System_Setting> map = DataTools.systemSettingMap;
		return obj;
	}
}

